package com.qsmx.store.app.main.goods.adapter.goodsorder;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qsmx.store.app.R;
import com.qsmx.store.app.event.GoodsOrderRefreshEvent;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.goods.adapter.GoodsCreateAdapter;
import com.qsmx.store.app.main.goods.adapter.GoodsOrderAdapter;
import com.qsmx.store.app.main.goods.entity.GoodsOrderEntity;
import com.qsmx.store.app.main.goods.entity.OrderGoodsEntity;
import com.qsmx.store.app.main.mem.entity.MemCoinsEntity;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;
import com.qsmx.store.core.util.dimen.DimenUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class GoodsOrderContentAdapter extends ArrayAdapter {

    private int itemResourceId;
    private List<GoodsOrderEntity.ContentBean> mOrderList;

    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //声明自定义的监听接口
    private GoodsOrderContentAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(GoodsOrderContentAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public void setList(List<GoodsOrderEntity.ContentBean> mOrderList) {
        this.mOrderList = mOrderList;
    }

    public GoodsOrderContentAdapter(@NonNull Context context, @LayoutRes int resource) {
        super(context, resource);
        this.itemResourceId = resource;
    }

    @Override
    public int getCount() {
        if (mOrderList != null && mOrderList.size() > 0) {
            return mOrderList.size();
        } else {
            return 0;
        }
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        ViewHolder viewHolder;

        GoodsOrderEntity.ContentBean data = mOrderList.get(position);

        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(itemResourceId, parent, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.tvOrderId.setText(data.getBillNo());
        viewHolder.tvOrderDate.setText(data.getUpdateTime());
        viewHolder.tvDoPerson.setText(data.getCreateBy());
        viewHolder.tvRemark.setText(data.getRemark());
        viewHolder.tvPayStatus.setText(String.valueOf(data.getPayStatus()));
        viewHolder.tvPayType.setText(String.valueOf(data.getPayMethod()));
        viewHolder.tvActualMoney.setText(String.valueOf(data.getActualMoney()));
        viewHolder.tvReceivableMoney.setText(String.valueOf(data.getReceivableMoney()));
        viewHolder.tvIcNum.setText(data.getMemberId());
        viewHolder.tvMemNum.setText(data.getMemberNo());

        viewHolder.tvOrderDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOrderDetailDialog(false, position);
            }
        });

        viewHolder.tvBackMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOrderDetailDialog(true, position);
            }
        });

        if (data.getStatus() == 0) {
            viewHolder.tvBackMoney.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvBackMoney.setVisibility(View.GONE);
        }

        return view;
    }

    private class ViewHolder {
        private AppCompatTextView tvOrderDetail;
        private AppCompatTextView tvBackMoney;
        private AppCompatTextView tvOrderId;
        private AppCompatTextView tvOrderDate;
        private AppCompatTextView tvDoPerson;
        private AppCompatTextView tvRemark;
        private AppCompatTextView tvPayStatus;
        private AppCompatTextView tvPayType;
        private AppCompatTextView tvReceivableMoney;
        private AppCompatTextView tvActualMoney;
        private AppCompatTextView tvIcNum;
        private AppCompatTextView tvMemNum;
        private AppCompatTextView tvBackOrder;

        ViewHolder(View itemView) {
            tvOrderDetail = itemView.findViewById(R.id.tv_order_detail);
            tvBackMoney = itemView.findViewById(R.id.tv_back_money);
            tvOrderId = itemView.findViewById(R.id.tv_order_id);
            tvOrderDate = itemView.findViewById(R.id.tv_order_date);
            tvDoPerson = itemView.findViewById(R.id.tv_do_person);
            tvRemark = itemView.findViewById(R.id.tv_remark);
            tvPayStatus = itemView.findViewById(R.id.tv_pay_status);
            tvPayType = itemView.findViewById(R.id.tv_pay_type);
            tvReceivableMoney = itemView.findViewById(R.id.tv_receivable_money);
            tvActualMoney = itemView.findViewById(R.id.tv_actual_money);
            tvIcNum = itemView.findViewById(R.id.tv_ic_card_num);
            tvMemNum = itemView.findViewById(R.id.tv_mem_id);
            tvBackOrder = itemView.findViewById(R.id.tv_back_money);
        }
    }

    private void showOrderDetailDialog(boolean isBack, final int position) {
        final Dialog dialog = new Dialog(getContext(), R.style.dialog);
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_order_detail);
        int width = (int) (DimenUtil.getScreenWidth() * 0.8);

        window.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);

        int wantBackMoney = 0;
        ConstraintLayout clBottom = window.findViewById(R.id.cl_bottom_content);
        final AppCompatEditText etWantBackMoney = window.findViewById(R.id.et_want_back_money);
        AppCompatTextView tvSureBack = window.findViewById(R.id.tv_sure_back);
        AppCompatTextView tvCancel = window.findViewById(R.id.tv_cancel);
        RecyclerView rlvGoodsXls = window.findViewById(R.id.rlv_goods_xls);
        GoodsOrderAdapter mGoodsAdapter = new GoodsOrderAdapter(getContext());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mGoodsAdapter.setData(mOrderList.get(position).getGoodsSellDetails());
        rlvGoodsXls.setAdapter(mGoodsAdapter);
        rlvGoodsXls.setLayoutManager(manager);

        for (GoodsOrderEntity.ContentBean.GoodsSellDetailsBean detail : mOrderList.get(position).getGoodsSellDetails()) {
            wantBackMoney = detail.getMoney() + wantBackMoney;
        }

        etWantBackMoney.setText(String.valueOf(wantBackMoney));

        if (isBack) {
            clBottom.setVisibility(View.VISIBLE);
        } else {
            clBottom.setVisibility(View.GONE);
        }

        tvSureBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject params = new JSONObject();
                params.put("deptId", FustionManager.mLoginData.getUser().getUser().getDept().getId());
                params.put("billNo", mOrderList.get(position).getBillNo());

                HttpHelper.RestClientPostRaw(params, HttpUrl.GOODS_REFUND,
                        new ISuccess() {
                            @Override
                            public void onSuccess(int code, String response) {
                                dialog.dismiss();
                                EventBus.getDefault().post(new GoodsOrderRefreshEvent(new Bundle()));
                            }
                        }, new IError() {
                            @Override
                            public void onError(int code, String msg) {

                            }
                        }, new IFailure() {
                            @Override
                            public void onFailure() {

                            }
                        });
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }
}
